# coding=utf-8
import maya.cmds as cmds
import maya.mel as mel
import os
#import core.ProductActivation as GetTime

class window_Switch_bin ():
	def __init__ ( self ):
		self.select = ''
		self.Left_HandVector = u'左手极向量'
		self.Left_HandIK = u'左手IK'
		self.Right_HandVector = u'右手极向量'
		self.Right_HandIK = u'右手IK'
		self.Left_HandElbow = u'左肘'
		self.Left_HandFK = u'左手FK'
		self.Right_HandElbow = u'右肘'
		self.Right_HandFK = u'右手FK'
	def Clear_history( self ):
		self.LocatorHistory = ['_Left_HandElbow_Tmp','_Left_HandFK_Tmp','_Right_HandElbow_Tmp','_Right_HandFK_Tmp']
		for LocatorHistoryTmp in self.LocatorHistory:
			for History in cmds.ls (transforms = 1):
				if LocatorHistoryTmp in History:
					cmds.delete (History)
				else:
					pass
	def IKFK_Switch_Win ( self,*args ):
		#if GetTime.ProductActivation().Win() ==0:
		#	return
		windowName = 'IKFK_Switch_Window'
		if cmds.window (windowName,ex = True):
			cmds.deleteUI (windowName)
		cmds.window (windowName)
		cmds.scrollLayout ('scrollLayout')
		# cmds.columnLayout( adjustableColumn=True )
		cmds.frameLayout (label = u'上肢',collapsable = 1,collapse = 1,marginWidth = 20,width = 600)
		cmds.frameLayout (label = u'IK',collapsable = 1,collapse = 1,marginWidth = 20)
		self.IKrowColumnLayout = cmds.rowColumnLayout (adjustableColumn = 1,numberOfColumns = 2,columnWidth=[(1, 250), (2, 250)])
		self.Left_HandVector_button = cmds.button (l = self.Left_HandVector)
		self.Left_HandIK_button = cmds.button (l = self.Left_HandIK)
		self.Right_HandVector_button = cmds.button (l = self.Right_HandVector)
		self.Right_HandIK_button = cmds.button (l = self.Right_HandIK)
		cmds.setParent ('..')
		cmds.setParent ('..')
		cmds.frameLayout (label = 'FK',collapsable = 1,collapse = 1,marginWidth = 20)
		self.FK_rowColumnLayout=cmds.rowColumnLayout (adjustableColumn = 1,numberOfColumns = 2,columnWidth=[(1, 250), (2, 250)])
		self.Left_HandElbow_button = cmds.button (l = self.Left_HandElbow)
		self.Left_HandFK_button = cmds.button (l = self.Left_HandFK)
		self.Right_HandElbow_button = cmds.button (l = self.Right_HandElbow)
		self.Right_HandFK_button = cmds.button (l = self.Right_HandFK)
		cmds.setParent ('..')
		cmds.setParent ('..')
		self.switch_button = cmds.button (l = u'切换')
		cmds.button (self.Left_HandVector_button,e = 1,c = self.Left_HandVector_button_min)
		cmds.button (self.Left_HandIK_button,e = 1,c = self.Left_HandIK_button_min)
		cmds.button (self.Right_HandVector_button,e = 1,c = self.Right_HandVector_button_min)
		cmds.button (self.Right_HandIK_button,e = 1,c = self.Right_HandIK_button_min)
		cmds.button (self.Left_HandElbow_button,e = 1,c = self.Left_HandElbow_button_min)
		cmds.button (self.Left_HandFK_button,e = 1,c = self.Left_HandFK_button_min)
		cmds.button (self.Right_HandElbow_button,e = 1,c = self.Right_HandElbow_button_min)
		cmds.button (self.Right_HandFK_button,e = 1,c = self.Right_HandFK_button_min)
		cmds.button (self.switch_button,e = 1, c =self.switch_button_min)
		cmds.window (windowName,e = 1,w = 600,h = 500,mxb=0, s = 0)
		self.Clear_history()
		cmds.showWindow (windowName)
		
	
	def Left_HandVector_button_min( self ,*args):
		self.select_controller()
		cmds.button (self.Left_HandVector_button,e = 1,ann = self.select,l = self.Left_HandVector + '>>>' + self.select)
	def Left_HandIK_button_min( self ,*args):
		self.select_controller ()
		cmds.button (self.Left_HandIK_button,e = 1,ann = self.select,l = self.Left_HandIK + '>>>' + self.select)
	def Right_HandVector_button_min( self,*args):
		self.select_controller ()
		cmds.button (self.Right_HandVector_button,e = 1,ann = self.select,l = self.Right_HandVector + '>>>' + self.select)
	def Right_HandIK_button_min( self ,*args):
		self.select_controller ()
		cmds.button (self.Right_HandIK_button,e = 1,ann = self.select,l = self.Right_HandIK + '>>>' + self.select)
	def Left_HandElbow_button_min( self ,*args):
		self.select_controller ()
		cmds.button (self.Left_HandElbow_button,e = 1,ann = self.select,l = self.Left_HandElbow + '>>>' + self.select)
	def Left_HandFK_button_min( self ,*args):
		self.select_controller ()
		cmds.button (self.Left_HandFK_button,e = 1,ann = self.select,l = self.Left_HandFK + '>>>' + self.select)
	def Right_HandElbow_button_min( self ,*args):
		self.select_controller ()
		cmds.button (self.Right_HandElbow_button,e = 1,ann = self.select,l = self.Right_HandElbow + '>>>' + self.select)
	def Right_HandFK_button_min( self ,*args):
		self.select_controller ()
		cmds.button (self.Right_HandFK_button,e = 1,ann = self.select,l = self.Right_HandFK + '>>>' + self.select)
		
	def switch_button_min ( self,*args ):
		self.Locator_tmp = []
		LocatorName = ''
		self.rowColumnLayout_FKbutton =cmds.rowColumnLayout(self.FK_rowColumnLayout,q = 1,childArray = 1)
		for FK_button_tmp in self.rowColumnLayout_FKbutton:
			select_FK_Button_AnnName = cmds.button (FK_button_tmp,q = 1,ann = 1)
			select_FK_Button_LName = cmds.button (FK_button_tmp,q = 1,l = 1)
			if self.Left_HandElbow in select_FK_Button_LName:
				LocatorName = cmds.spaceLocator (n = select_FK_Button_AnnName + '_Left_HandElbow_Tmp')
				cmds.parentConstraint (select_FK_Button_AnnName,LocatorName,w = 1)
			elif self.Left_HandFK in select_FK_Button_LName:
				LocatorName = cmds.spaceLocator (n = select_FK_Button_AnnName + '_Left_HandFK_Tmp')
				cmds.parentConstraint (select_FK_Button_AnnName,LocatorName,w = 1)
			elif self.Right_HandElbow in select_FK_Button_LName:
				LocatorName = cmds.spaceLocator (n = select_FK_Button_AnnName + '_Right_HandElbow_Tmp')
				cmds.parentConstraint (select_FK_Button_AnnName,LocatorName,w = 1)
			elif self.Right_HandFK in select_FK_Button_LName:
				LocatorName = cmds.spaceLocator (n = select_FK_Button_AnnName + '_Right_HandFK_Tmp')
				
				cmds.parentConstraint (select_FK_Button_AnnName,LocatorName,w = 1)
			for LocatorName in LocatorName:
				mel.eval ('setAttr ' + LocatorName + '.hiddenInOutliner 1;AEdagNodeCommonRefreshOutliners();')
				self.Locator_tmp.append(LocatorName)
		cmds.bakeResults (self.Locator_tmp,t = (self.starFram,self.endFram),simulation = 1,sampleBy = 1,oversamplingRate = 1,disableImplicitControl = 1,preserveOutsideKeys = 1,sparseAnimCurveBake = 0,removeBakedAttributeFromLayer = 0,removeBakedAnimFromLayer = 0,bakeOnOverrideLayer = 0,minimizeRotation = 1,controlPoints = 0,shape = 1)
		for Locator_Tmp in self.Locator_tmp:
			cmds.delete (Locator_Tmp,constraints = 1)
		self.rowColumnLayout_IKbutton = cmds.rowColumnLayout (self.IKrowColumnLayout,q = 1,childArray = 1)
		self.select_IK_Button_AnnName_set =[]
		for IK_button_tmp in self.rowColumnLayout_IKbutton:
			select_IK_Button_LName =cmds.button (IK_button_tmp,q = 1,l = 1)
			self.select_IK_Button_AnnName = cmds.button (IK_button_tmp,q = 1,ann = 1)
			self.select_IK_Button_AnnName_set.append(self.select_IK_Button_AnnName)
			for Locator_Tmp in self.Locator_tmp:
				if self.Left_HandVector in select_IK_Button_LName:
					if 'Left_HandElbow' in Locator_Tmp:
						cmds.pointConstraint (Locator_Tmp,self.select_IK_Button_AnnName,w = 1)
				elif self.Right_HandVector in select_IK_Button_LName:
					if 'Right_HandElbow' in Locator_Tmp:
						cmds.pointConstraint (Locator_Tmp,self.select_IK_Button_AnnName,w = 1)
				elif self.Left_HandIK in select_IK_Button_LName:
					if 'Left_HandFK' in Locator_Tmp:
						cmds.parentConstraint (Locator_Tmp,self.select_IK_Button_AnnName,w = 1)
				elif self.Right_HandIK in select_IK_Button_LName:
					if 'Right_HandFK' in Locator_Tmp:
						cmds.parentConstraint (Locator_Tmp,self.select_IK_Button_AnnName,w = 1)
				
		cmds.bakeResults (self.select_IK_Button_AnnName_set,t = (self.starFram,self.endFram),simulation = 1,sampleBy = 1,oversamplingRate = 1,disableImplicitControl = 1,preserveOutsideKeys = 1,sparseAnimCurveBake = 0,removeBakedAttributeFromLayer = 0,removeBakedAnimFromLayer = 0,bakeOnOverrideLayer = 0,minimizeRotation = 1,controlPoints = 0,shape = 1)
		cmds.delete (self.select_IK_Button_AnnName_set,constraints = 1)
		cmds.delete (self.Locator_tmp)
	def select_controller( self,*args):
		self.selectTmp = cmds.ls (sl = 1)
		for self.select in self.selectTmp:
			self.select 
		self.starFram = int (cmds.playbackOptions (q = 1,min = 1))
		self.endFram = int (cmds.playbackOptions (q = 1,max = 1))
#window_Switch_bin ().IKFK_Switch_Win ()